package com.hzsai.controller;

import com.hzsai.pojo.User;
import com.hzsai.service.Impl.UserValidatorServiceImpl;
import com.hzsai.service.UserValidatorService;
import com.hzsai.service.UserService;
import com.hzsai.utils.ResponseMsg;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class ValidatorTestController implements ApplicationContextAware{

    @Autowired
    private UserService userService;

    private ApplicationContext context;

    @GetMapping("/validate")
    public ResponseMsg validateAndPrint(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        // 在MyAspect里面有设置DeclareParents，是外部实现继承，在外部用aop的方式进行继承，拓展父类功能
        // 在这里，拓展userService，将父类service强制转换为子类，使其有子类功能？？？
        // 有功能，但是类的变量都是空的！！！！！！
        //
        UserValidatorService validator = (UserValidatorService)userService;
        System.out.println(validator.getClass().getInterfaces());

        if (validator.validateUserExist(user, userService.getUserDAO())) {
            System.out.println("name: " + username + " " + "password: " + password);
            userService.insertUser(username, password);
            return ResponseMsg.success(200, "验证成功，成功插入", user);
        }
        return ResponseMsg.fail(200, "验证错误", null);
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
